import pandas as pd
import numpy as np
import re
import queue
q=queue.Queue(10000)
ALl_data = pd.DataFrame()  

CAN_data = pd.Series({'AIdxChn':0,'AIsTx':True,'AIsRemote':False,'AIsExtended':False,
                    'AIsError':False,'ADLC':8,'AIdentifier':0x114,'ATimeUs':1111111/1000000.0,'ADatas':[0x88,0x00,0x7,0x00,0x00,0x00,0x00,0x00]})
CAN_data1 = pd.Series({'AIdxChn':0,'AIsTx':True,'AIsRemote':False,'AIsExtended':False,
                    'AIsError':False,'ADLC':8,'AIdentifier':0x114,'ATimeUs':2222222/1000000.0,'ADatas':[0x00,0x74,0x65,0x73,0x74,0x20,0x31,0x32]})
CAN_data2 = pd.Series({'AIdxChn':0,'AIsTx':True,'AIsRemote':False,'AIsExtended':False,
                    'AIsError':False,'ADLC':8,'AIdentifier':0x120,'ATimeUs':3333333/1000000.0,'ADatas':[0x88,0x00,0x09,0x00,0x00,0x00,0x00,0x00]}) 
CAN_data3 = pd.Series({'AIdxChn':0,'AIsTx':True,'AIsRemote':False,'AIsExtended':False,
                    'AIsError':False,'ADLC':8,'AIdentifier':0x114,'ATimeUs':4444444/1000000.0,'ADatas':[0x01,0x74,0x65,0x73,0x74,0x20,0x31,0x32]})
CAN_data4 = pd.Series({'AIdxChn':0,'AIsTx':True,'AIsRemote':False,'AIsExtended':False,
                    'AIsError':False,'ADLC':8,'AIdentifier':0x121,'ATimeUs':5555555/1000000.0,'ADatas':[0x88,0x00,0x09,0x00,0x00,0x00,0x00,0x00]})                     
CAN_data5 = pd.Series({'AIdxChn':0,'AIsTx':True,'AIsRemote':False,'AIsExtended':False,
                    'AIsError':False,'ADLC':8,'AIdentifier':0x114,'ATimeUs':6666666/1000000.0,'ADatas':[0xFF,0x00,0x09,0x00,0x00,0x00,0x00,0x00]})
                   
q.put(CAN_data)
q.put(CAN_data1)
q.put(CAN_data2)
q.put(CAN_data3)
q.put(CAN_data4)
q.put(CAN_data5)

cmd_str = 'test'
if q.qsize()>0:
    for i in range(q.qsize()):
        ALl_data = ALl_data.append(q.get(),ignore_index=True)
        pass

    Cal_data = ALl_data[ALl_data['AIdentifier']==0x114]

    for i in Cal_data.index: #search first frame
        if Cal_data.loc[i].ADatas[0] ==0x88:
            start_valid = True
            start = i
            cmd_len = ((Cal_data.loc[i].ADatas[1]) << 8) + Cal_data.loc[i].ADatas[2]
            break
        else:
            start_valid = False
    for i in Cal_data.index:#search last frame
        if Cal_data.loc[i].ADatas[0] ==0xFF:
            stop_valid = True
            stop = i
            break
        else:
            stop_valid = False
    if start_valid and stop_valid: #search the replay value
        new = Cal_data.iloc[Cal_data.index.get_loc(start)+1:Cal_data.index.get_loc(stop),:]
        replay = []
        for index, row in new.iterrows():
            replay += row.ADatas[1:]
        last_value = replay[:cmd_len]
        value_str = bytes(last_value).decode('ascii')
        if len(re.findall('(?<={0} ).*$'.format(cmd_str),value_str)) >0 :
            replay_value = int(re.findall('(?<={0} ).*$'.format(cmd_str),value_str)[0])
        else:
            replay_value = 0
    else:
        replay_value = 0
print(replay_value)

cmd_str = 'test 12'* 3 +'s11d'
if len(cmd_str)%7 == 0:
    cmd_array = list(bytearray(cmd_str.encode('ascii')))
else:
    cmd_array = list(bytearray(cmd_str.encode('ascii'))) + [0x00]*(7-len(cmd_str)%7)
new_array = np.array(cmd_array).reshape(-1,7)
index_array = np.arange(new_array.shape[0])
send_array = np.insert(new_array,0,index_array,axis=1)

first_frame = [0x88] + [len(cmd_array)>>8] + [(len(cmd_array))%255]+[0,0,0,0,0]
last_frame = [0xFF] + [0,0,0,0,0,0,0]
print(first_frame)
print(last_frame)
for i in send_array:
    print(i)



aa= [4,7.6,-3.8,-65.25,44,0,9.89,-44,0,0,0,32767,49,1,32,0,0.1]


ttt = [0,0,0,0,0,0,0,0]

ttt[0] = int(aa[0])
ttt[1] = (int(aa[1]*20)&0x1FFF) >>5
ttt[2] = ((int(aa[1]*20)&0x1F)<<3) + ((int(aa[2]*10)&0x7FF)>>8)
ttt[3] = (int(aa[2]*10)&0xFF)
ttt[4] = (int(aa[3]*4)&0x3FF)>>2
ttt[5] = ((int(aa[3]*4)&0x03)<<6) +((int(aa[7])&0x1FF)>>3)
ttt[6] = ((int(aa[7])&0x07)<<5) + 0x00
ttt[7] = int(aa[4]*2)&0xFF

for bbb in ttt:
    print('0x%x'%bbb)
    
tttt = pd.DataFrame()

CAN1 = pd.Series({'AIdxChn':1,'AIsTx':2,'AIsRemote':3,'AIsExtended':4,'AIsError':5,
                        'ADLC':6,'AIdentifier':7,'ATimeUs':8,'ADatas':9
                        })

CAN2 = pd.Series({'AIdxChn':11,'AIsTx':12,'AIsRemote':13,'AIsExtended':14,'AIsError':15,
                        'ADLC':16,'AIdentifier':17,'ATimeUs':18,'ADatas':19
                        })
CAN3 = pd.Series({'AIdxChn':21,'AIsTx':22,'AIsRemote':23,'AIsExtended':24,'AIsError':25,
                        'ADLC':26,'AIdentifier':27,'ATimeUs':28,'ADatas':29
                        })

tttt = pd.concat([tttt,CAN1.to_frame().T],ignore_index=True)
tttt = pd.concat([tttt,CAN2.to_frame().T],ignore_index=True)
tttt = pd.concat([tttt,CAN3.to_frame().T],ignore_index=True)
print(tttt)
print(tttt['AIdxChn'].values)